/*
 * @autor: Vânia Angelotti RA: 1430431112040
 */

package br.fatec.controller;

import br.fatec.model.Sac;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/cadastroSac") //informa qual e a controler para salvar
public class CadastroSacController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        Sac chamado = new Sac();
        chamado.setNome(req.getParameter("nome"));
        chamado.setSobrenome(req.getParameter("sobrenome"));
        chamado.setProduto(req.getParameter("produto"));
        chamado.setGrau(req.getParameter("grau"));
        chamado.setComentario(req.getParameter("comentario"));
        chamado.setSexo(req.getParameter("radioSexo"));
        chamado.setResposta(req.getParameter("ckekResposta"));
        
        try {
            DateFormat df = DateFormat.getDateInstance(DateFormat.DATE_FIELD, new Locale("pt", "BR"));
            chamado.setData(df.parse(req.getParameter("data")));
        } catch (ParseException ex) {
            Logger.getLogger(CadastroSacController.class.getName()).log(Level.SEVERE, null, ex);
        }
                req.getRequestDispatcher("/gerarResposta").forward(req, res);  

        EntityManager manager = Persistence.createEntityManagerFactory("trabalhoppi2").createEntityManager();
        EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        
        manager.persist(chamado);
        transaction.commit();
        
        req.setAttribute("modeloGerado", chamado);
        req.getRequestDispatcher("/gerarResposta").forward(req, res);  
    }
}
